package com.time.workbench.efficient.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.time.workbench.core.util.AuthUtil;
import com.time.workbench.efficient.entity.bo.Environment;
import com.time.workbench.efficient.entity.bo.EnvironmentServer;
import com.time.workbench.efficient.entity.bo.Server;
import com.time.workbench.efficient.entity.dto.EnvironmentPageReq;
import com.time.workbench.efficient.entity.dto.SetEnvironmentServerReq;
import com.time.workbench.efficient.service.EnvironmentServerService;
import com.time.workbench.efficient.service.EnvironmentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 环境管理
 *
 * @description: 环境管理
 * @menu 环境管理
 */
@RestController
@RequestMapping("/api/efficient/environment")
@Slf4j
@RequiredArgsConstructor
public class EnvironmentController {
    /**
     * 环境
     */
    private final EnvironmentService environmentService;

    /**
     * 环境服务器关系
     */
    private final EnvironmentServerService environmentServerService;


    /**
     * 获取所有环境
     * @return
     */
    @GetMapping("/getAll")
    public List<Environment> getAll(){
        return environmentService.list();
    }

    /**
     * 获取环境所有服务器关系
     *
     * @param environmentId
     * @return
     */
    @GetMapping("/getByEnvironmentId/{id}")
    public List<EnvironmentServer> getByEnvironmentId(@PathVariable("id") Integer environmentId) {
        return environmentServerService.getByEnvironmentId(environmentId);
    }


    @PostMapping("/setServer/{id}")
    public void setServer(@RequestBody @Validated SetEnvironmentServerReq setEnvironmentServerReq, @PathVariable("id") Integer environmentId) {
        environmentServerService.setServer(setEnvironmentServerReq, environmentId);
    }

    /**
     * 分页查询
     *
     * @param environmentPageReq
     * @return
     * @status done
     */
    @GetMapping("/page")
    public Page<Environment> page(@Validated EnvironmentPageReq environmentPageReq) {
        return environmentService.page(environmentPageReq);
    }

    /**
     * 保存环境
     *
     * @param environment
     * @status done
     */
    @PostMapping("")
    public void save(@RequestBody @Validated Environment environment) {
        environment.setTenantId(AuthUtil.getCurrTenantId());
        environmentService.save(environment);
    }

    /**
     * 修改环境
     *
     * @param environment
     * @param id
     * @status done
     */
    @PutMapping("/{id}")
    public void update(@RequestBody @Validated Environment environment, @PathVariable("id") Integer id) {
        environment.setId(id);
        environmentService.updateById(environment);
    }

    /**
     * 删除环境
     *
     * @status done
     */
    @DeleteMapping("/{id}")
    public void remove(@PathVariable("id") Integer id) {
        environmentService.removeById(id);
    }
}
